var agent = navigator.userAgent.toLowerCase();
var ie = false;
var is_ie = ((agent.indexOf("msie") != -1) && (agent.indexOf("opera") == -1));
var is_ie6 = (is_ie && (parseInt(navigator.appVersion) == 4) && (agent.indexOf("msie 6.") != -1));
var is_ie7 = (is_ie && (parseInt(navigator.appVersion) == 4) && (agent.indexOf("msie 7.") != -1));
if(is_ie && !is_ie7) { var ie = true; }

var messengerWin;

if(document.domain.indexOf("e-majandus.localhost") != -1)
{
	var domain = 'http://e-majandus.localhost/';
	var static = 'http://static.e-majandus.localhost/';
	var img_domain = 'http://static.e-majandus.localhost/img/';
}
else
{
	var domain = 'http://e-majandus.ee/';
// 	var static = 'http://static.e-majandus.ee/';
// 	var img_domain = 'http://static.e-majandus.ee/img/';
	var static = 'http://emajandus_static.puu.ee/';
	var img_domain = 'http://emajandus_static.puu.ee/img/';
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function messenger()
{
// 	messengerWin = window.open(domain + 'Kiirsuhtlus/', "messenger", "location=no,menubar=no,directories=no,toolbar=no,status=no,resizable=yes,scrollbars=yes,width=1000,height=500");
	if(ie)
	{
		messengerWin = window.open(domain + 'Kiirsuhtlus/', "messenger", "location=no,menubar=no,directories=no,toolbar=no,status=no,resizable=yes,scrollbars=yes,width=950,height=575");
	}
	else
	{
		messengerWin = window.open(domain + 'Kiirsuhtlus/', "messenger", "location=no,menubar=no,directories=no,toolbar=no,status=no,resizable=yes,scrollbars=yes,innerWidth=910,innerHeight=530");
	}

	return false;
}

function modals()
{
	jQuery('#top .icon a').bind('click', function(e)
	{
		var cl = jQuery(this).parents('li').attr('class').replace(/icon /, '');
		var title = '';
		var file = '';

		if(cl == "teated")
		{
			title = 'Teated';
			file = 'sonumid';
		}
		else if(cl == "kalender")
		{
			title = 'Kalender';
			file = 'kalender';
		}
		else if(cl == "arved")
		{
			title = 'Arved';
			file = 'arved';
		}
		else if(cl == "messenger")
		{
			title = "E-majandus messenger";
			file = 'messenger';
		}
		else if(cl == "intranet")
		{
			title = "Intranet";
			file = 'intranet';
		}
		else if(cl == "ostukorv")
		{
			title = "Ostukorv";
			file = 'ostukorv';
		}
		else if(cl == "admin")
		{
			title = "Seaded";
			file = 'seaded';
		}
		else if(cl == "minue")
		{
			title = "Minu E";
			file = 'minue';
		}

		var d = '';

		if(file)
		{
			d = '<div class="right"><img src="'+img_domain+'tour/'+file+'.png" alt="Pilt" /></div>';
		}

		d += '<div class="left">';
		d += '	<h1>'+title+'</h1>';
		d += '	<p>Selle funktsiooni kasutamiseks pead <a href="'+domain+'">sisse logima!</a></p>';
		d += '	<p>Pole kontot? <a href="'+domain+'Registreeri/">Registreeri!</a></p>';
		d += '</div>';

		jQuery(d).modal();
		return false;
	});
}


var multiFile = {
	maxFiles: 0,
	labels: '#labels',
	fileInput: '#file-button input',
	callback: null,
	init: function(config)
	{
		if(config && typeof config == "object")
		{
			for(var a in config)
			{
				this[a] = config[a];
			}
		}

		var obj = this;
		jQuery(this.fileInput).change(function()
		{
			obj.multiFile(this);
		});
	},
	multiFile: function(file)
	{
		var obj = this;

		if(this.callback)
		{
			this.callback();
		}

		if(this.maxFiles && jQuery('#file input').size() > this.maxFiles)
		{
			alert('Ühe korraga saab üles laadida maksimaalselt '+this.maxFiles+' faili.');
			file.value = '';
			return;
		}

		jQuery(file).hide();
		jQuery(file).parent().prepend('<input type="file" name="fail[]" class="file" />')
			.find('input').change(function() { obj.multiFile(this); });

		if(file.value)
		{
			jQuery(this.labels).append('<li>'+file.value+' <a href="#">[x]</a></li>')
				.find('a').click(function()
					{
						jQuery(this).parent().remove();
						jQuery(file).remove();
						return false;
					});
		}
	}
};



var lightBoxSettings = {
	fixedNavigation: false,
	imageLoading:  img_domain+'lightbox-ico-loading.gif',
	imageBtnPrev:  img_domain+'lightbox-btn-prev.gif',
	imageBtnNext:  img_domain+'lightbox-btn-next.gif',
	imageBtnClose: img_domain+'lightbox-btn-close.gif',
	imageBlank:    img_domain+'lightbox-blank.gif'
};
